package com.longcai.piaojia.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.longcai.piaojia.R;

/**
 * Created by Administrator on 2017/5/22.
 */

public class IndexChainActivity extends BaseActivity implements View.OnClickListener {


    WebView web_wv;

    @SuppressLint("JavascriptInterface")

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chain);
        web_wv = (WebView) findViewById(R.id.web_wv);
        // 支持js
        Bundle bundle = getIntent().getExtras();

        String url = bundle.getString("url", "");
        web_wv.getSettings().setJavaScriptEnabled(true);
        web_wv.setWebChromeClient(new WebChromeClient());
        web_wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        // 缩放
        web_wv.getSettings().setSupportZoom(true);
        web_wv.getSettings().setBuiltInZoomControls(false);

        web_wv.addJavascriptInterface(this, "android");
        web_wv.getSettings().setJavaScriptEnabled(true);
        web_wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        web_wv.setWebViewClient(new IndexChainActivity.MyWebViewClient());

        web_wv.loadUrl(url);
        Log.d(url,"3333333333333333333");


        setTitleName(bundle.getString("title"));
  /*      web_wv.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {

                web_wv.loadUrl("javascript:getvalue()");

                super.onPageFinished(view, url);
            }

        });*/

//        String url = getIntent().getStringExtra("url");
       /* setTitleName(bundle.getString("title"));
        if (!TextUtils.isEmpty(url)) {

            web_wv.loadUrl(url);

        } else {

            finish();
        }*/
        setBackTrue();
    }

    @Override
    public void onClick(View v) {

    }


    private class MyWebViewClient extends WebViewClient {

        @Override
        // 在WebView中而不是默认浏览器中显示页面
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            // 处理电话 短信
            if (url.indexOf("tel:") == 0) {
                String telno = url.substring(4).toString();

                Intent intent = new Intent();
                intent.setAction("android.intent.action.DIAL");
                intent.setData(Uri.parse("tel:" + telno));
                startActivity(intent);

                // Intent intent2 = new Intent(Intent.ACTION_CALL,
                // Uri.parse("tel:" + telno));
                // startActivity(intent2);
            } else if (url.indexOf("sms:") == 0) {
                String smslno = url.substring(4).toString();
                Uri uri = Uri.parse("smsto:" + smslno);
                Intent intent1 = new Intent(Intent.ACTION_SENDTO, uri);
                intent1.putExtra("sms_body", "网站留言：");
                startActivity(intent1);
            } else {
                view.loadUrl(url);
            }
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            // view.loadData(errorHtml, "text/html", "UTF-8");

//            web_wv.setBackgroundColor(0);
//            web_wv.setBackgroundResource(R.drawable.wuwangluo);
//
//            view.loadUrl("file:///android_asset/error.html");
//
//            Toast.makeText(getApplicationContext(), "Sorry 网络不通...",
//                    Toast.LENGTH_SHORT).show();

        }

        @Override
        // 加载完成时要做的工作
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);


        }

    }
}
